<?php
class PagesController extends AppController {

	var $name = 'Pages';
	var $uses = array('Page', 'Pothole', 'Metro', 'Town');
	var $helpers = array('Html', 'Form', 'Ajax', 'Javascript', 'GoogleMap', 'Text' );
	var $components = array('RequestHandler', 'Simplepie');
	
	// Cookies:
	function beforeFilter() {
		$this->cookieMonster();
	}

	function view($page = 'home')
		{
			$this->set('showPage', $page);
			// If a method exists that matches the name of the page, pass info off to that function before calling the view
			if (method_exists($this, $page)) {
				$this->$page($page);
				$this->render('home');
			} else {
				$this->set('pages', $this->Page->findAll());
			}
			// Get session variables
			$this->set('cookie_data', $this->Cookie->read());
		}
	function index() {
	
	}
	
	function add()
		{
		if (!empty($this->data['Page']))
			{
				$this->data['Page']['ID'] = $this->Uuid->generate();
				$this->data['Page']['name'] = $this->makeSlug($this->data['Page']['title'], $length = 100, $replacement = '-');
				if($this->Page->save($this->data['Page']))
					{
						$this->flash('Your Page has been created.','/pages/');
					}
			}
		}
		
	function edit()
		{
		if (!empty($this->data['Page']))
			{
				$this->data['Page']['ID'] = $this->Uuid->generate();
				$this->data['Page']['name'] = implode('_', explode(' ', strtolower(preg_replace('/\W/', '', $this->data['Page']['title']))));
				if($this->Page->save($this->data['Page']))
					{
						$this->flash('Your Page has been created.','/pages/');
					}
			}
		}
	
	/*///////////////////////////////////////////
	//  Method: home                           //
	//  Params: none                           //
	//  Use: Loads data relevant to displaying //
	//       the home page.                    //
	///////////////////////////////////////////*/
	function home() {
		$this->set('potholes', $this->Pothole->find('all'));
		$this->set('metros', $this->Metro->find('all'));
		// Get session variables
		$this->set('cookie_data', $this->Cookie->read());
		App::import('Core',  'Xml');
		$wordpress = new XML('http://blog.potholepatrol.org/feed/');
		$this->set('feed', $wordpress->toArray()); 
	}
}
?>
